﻿Imports Microsoft.DPE.Samples.PictureProvider.Configuration
Imports System.Configuration
Imports System.Configuration.Provider
Imports System.Web.Configuration


Public NotInheritable Class PictureManager


#Region "Initialization"




    Private Shared isInitialized As Boolean

    Private Shared initializationException As Exception

    Private Shared initializationLock As New Object()

    Private Shared defaultProvider As PictureProvider

    Public Shared ReadOnly Property Provider() As PictureProvider
        Get
            Initialize()
            Return defaultProvider
        End Get
    End Property

    Private Shared providerCollection As PictureProviderCollection
    Public Shared ReadOnly Property Providers() As PictureProviderCollection
        Get
            Initialize()
            Return providerCollection
        End Get
    End Property

    Private Shared Sub Initialize()
        If isInitialized Then
            If initializationException IsNot Nothing Then
                Throw initializationException
            End If
        Else

            If initializationException IsNot Nothing Then
                Throw initializationException
            End If

            SyncLock initializationLock
                If isInitialized Then
                    If initializationException IsNot Nothing Then
                        Throw initializationException
                    End If
                Else

                    Try
                        'Get the feature's configuration info
                        Dim qc As PictureProviderSection = DirectCast(ConfigurationManager.GetSection("pictures"), PictureProviderSection)

                        If qc.DefaultProvider Is Nothing OrElse qc.Providers Is Nothing OrElse qc.Providers.Count < 1 Then
                            Throw New ProviderException("You must specify a valid default provider.")
                        End If

                        'Instantiate the providers
                        providerCollection = New PictureProviderCollection()
                        ProvidersHelper.InstantiateProviders(qc.Providers, providerCollection, GetType(PictureProvider))
                        providerCollection.SetReadOnly()
                        defaultProvider = providerCollection(qc.DefaultProvider)
                        If defaultProvider Is Nothing Then
                            Throw New ConfigurationErrorsException("You must specify a default provider for the feature.", _
                                                                   qc.ElementInformation.Properties(PictureProviderSection.STR_DefaultProvider).Source, _
                                                                   qc.ElementInformation.Properties(PictureProviderSection.STR_DefaultProvider).LineNumber)
                        End If
                    Catch ex As Exception

                        initializationException = ex
                        Throw
                    End Try

                    isInitialized = True
                End If
            End SyncLock
        End If
    End Sub




#End Region


    ''' <summary>
    ''' Gets the specified Picture from the data store
    ''' </summary>
    Public Shared Function GetPictureById(ByVal pictureId As String) As Picture
        If String.IsNullOrEmpty(pictureId) Then
            Throw (New ArgumentNullException("pictureId"))
        End If

        Return (Provider.GetPictureById(pictureId))
    End Function

    ''' <summary>
    ''' Gets a collection of all the pictures in the data store
    ''' </summary>
    Public Shared Function GetPictures() As List(Of Picture)
        Return Provider.GetPictures()
    End Function

    ''' <summary>
    ''' Gets a collection of pictures filtered by tag
    ''' </summary>
    Public Shared Function GetPicturesByTag(ByVal tagName As String) As List(Of Picture)
        If String.IsNullOrEmpty(tagName) Then
            Throw (New ArgumentNullException("tagName"))
        End If

        Return (Provider.GetPicturesByTag(tagName))
    End Function
End Class
